package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.aftersales.bean.ShRepairUpkeep;
import com.jy.adscm.operation.aftersales.bean.ShRepairUpkeepPartsMaterialList;
import com.jy.adscm.operation.aftersales.bean.ShSpaceParts;
import com.jy.adscm.operation.aftersales.bean.ShSpacePartsDetail;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairUpkeepMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairUpkeepPartsMaterialListMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShSpacePartsDetailMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShSpacePartsMapper;
import com.jy.adscm.operation.aftersales.service.ShRepairUpkeepPartsMaterialListService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.bean.JqbzSettings;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzSettingsMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-29
 */
@Service
public class ShRepairUpkeepPartsMaterialListServiceImpl extends ServiceImpl<ShRepairUpkeepPartsMaterialListMapper, ShRepairUpkeepPartsMaterialList> implements ShRepairUpkeepPartsMaterialListService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
    @Autowired
    ShSpacePartsMapper shSpacePartsMapper;
    @Autowired
    ShSpacePartsDetailMapper shSpacePartsDetailMapper;
    @Autowired
    ShRepairUpkeepMapper shRepairUpkeepMapper;
    @Override
    public boolean insertShRepairUpkeepPartsMaterialList(ShRepairUpkeepPartsMaterialList shRepairUpkeepPartsMaterialList) {

        ShRepairUpkeep shRepairUpkeep = shRepairUpkeepMapper.selectById(shRepairUpkeepPartsMaterialList.getPid());
        //备件出库
        ShSpaceParts shSpaceParts = shSpacePartsMapper.selectById(shRepairUpkeepPartsMaterialList.getKid());
        shSpaceParts.setState(1);
        shSpacePartsMapper.updateById(shSpaceParts);
        shRepairUpkeepPartsMaterialList.setCode(shSpaceParts.getPartsCode());
        shRepairUpkeepPartsMaterialList.setBatch(shSpaceParts.getPartsBatchCode());
        shRepairUpkeepPartsMaterialList.setType(shSpaceParts.getPartsType());
        shRepairUpkeepPartsMaterialList.setNum(1);
        ShSpacePartsDetail shSpacePartsDetail =new ShSpacePartsDetail();
        shSpacePartsDetail.setCreator(shRepairUpkeepPartsMaterialList.getCreator());
        shSpacePartsDetail.setMachine(shRepairUpkeep.getMachine().toString());
        shSpacePartsDetail.setCustomer(shRepairUpkeep.getCustomer().toString());
        shSpacePartsDetail.setPid(shSpaceParts.getId());
        shSpacePartsDetail.setCreateTime(new Date());
        shSpacePartsDetailMapper.insert(shSpacePartsDetail);
        boolean insert = super.insert(shRepairUpkeepPartsMaterialList);
        return insert;
    }

    @Override
    public boolean deleteShRepairUpkeepPartsMaterialList(Integer id) {
        //查询用料
        ShRepairUpkeepPartsMaterialList shRepairUpkeepPartsMaterialList = super.selectById(id);
        //重新入库
        ShSpaceParts shSpaceParts = shSpacePartsMapper.selectById(shRepairUpkeepPartsMaterialList.getKid());
        shSpaceParts.setState(0);
        shSpacePartsMapper.updateById(shSpaceParts);
        Wrapper<ShSpacePartsDetail> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",shSpaceParts.getId());
        shSpacePartsDetailMapper.delete(wrapper);
        boolean b = super.deleteById(id);
        return b;
    }

    @Override
    public List<ShRepairUpkeepPartsMaterialList> selectByPid(Integer pid) {
        Wrapper<ShRepairUpkeepPartsMaterialList> wrapper =new EntityWrapper<>();
        wrapper.eq("pid",pid);
        List<ShRepairUpkeepPartsMaterialList> shRepairUpkeepPartsMaterialLists = super.selectList(wrapper);
        for (ShRepairUpkeepPartsMaterialList shRepairUpkeepPartsMaterialList : shRepairUpkeepPartsMaterialLists) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeepPartsMaterialList.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(shRepairUpkeepPartsMaterialList.getCreator());
                shRepairUpkeepPartsMaterialList.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeepPartsMaterialList.getName(),null)){
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(shRepairUpkeepPartsMaterialList.getName());
                shRepairUpkeepPartsMaterialList.setNameName(jqbzSettings.getName());
            }

        }
        return shRepairUpkeepPartsMaterialLists;
    }


}
